package com.wcpdoc.survey.survey.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.wcpdoc.survey.core.dao.BaseDao;
import com.wcpdoc.survey.core.entity.LoginUser;
import com.wcpdoc.survey.core.service.impl.BaseServiceImp;
import com.wcpdoc.survey.core.util.ValidateUtil;
import com.wcpdoc.survey.survey.dao.QuestionDao;
import com.wcpdoc.survey.survey.entity.Question;
import com.wcpdoc.survey.survey.entity.QuestionType;
import com.wcpdoc.survey.survey.service.QuestionService;
import com.wcpdoc.survey.survey.service.QuestionTypeService;
import com.wcpdoc.survey.sys.cache.DictCache;

/**
 * 问题服务层实现
 * 
 * v1.0 zhanghc 2017-05-07 14:56:29
 */
@Service
public class QuestionServiceImpl extends BaseServiceImp<Question> implements QuestionService {
	@Resource
	private QuestionDao questionDao;
	@Resource
	private QuestionTypeService questionTypeService;

	@Override
	@Resource(name = "questionDaoImpl")
	public void setDao(BaseDao<Question> dao) {
		super.dao = dao;
	}


	@Override
	public void saveAndUpdate(Question question) {
		//校验数据有效性
		if(question.getQuestionTypeId() == null){
			throw new RuntimeException("无法获取参数： questionTypeId");
		}
		if(question.getType() == null){
			throw new RuntimeException("无法获取参数： type");
		}
		//1：单选竖向；2：单选横向；3：单选行列式；4：多选竖向；5：多选横向；6：多选行列式；7：下拉列表；8：下拉列表行列式；9：输入框；10：文本框
		int maxOptionSize = DictCache.getIndexDictlistMap().get("QUESTION_OPTIONS").size();
		if(question.getType() == 1 || question.getType() == 2 || question.getType() == 4 
				|| question.getType() == 5 || question.getType() == 7){
			if(!ValidateUtil.isValid(question.getOptionA())){
				throw new RuntimeException("无法获取参数：optionA");
			}
			
			String[] optionAArr = question.getOptionA().split("\n");
			if(optionAArr.length > maxOptionSize){
				throw new RuntimeException("最多" + maxOptionSize + "个选项");
			}
		}
		if(question.getType() == 3 || question.getType() == 6){
			if(!ValidateUtil.isValid(question.getOptionA())){
				throw new RuntimeException("无法获取参数：optionA");
			}
			if(!ValidateUtil.isValid(question.getOptionB())){
				throw new RuntimeException("无法获取参数：optionB");
			}
			
			String[] optionAArr = question.getOptionA().split("\n");
			if(optionAArr.length > maxOptionSize){
				throw new RuntimeException("最多" + maxOptionSize + "个选项");
			}
			String[] optionBArr = question.getOptionB().split("\n");
			if(optionBArr.length > maxOptionSize){
				throw new RuntimeException("最多" + maxOptionSize + "个选项");
			}
		}
		if(question.getType() == 8){
			if(!ValidateUtil.isValid(question.getOptionA())){
				throw new RuntimeException("无法获取参数：optionA");
			}
			if(!ValidateUtil.isValid(question.getOptionB())){
				throw new RuntimeException("无法获取参数：optionB");
			}
			if(!ValidateUtil.isValid(question.getOptionC())){
				throw new RuntimeException("无法获取参数：optionC");
			}
			
			String[] optionAArr = question.getOptionA().split("\n");
			if(optionAArr.length > maxOptionSize){
				throw new RuntimeException("最多" + maxOptionSize + "个选项");
			}
			String[] optionBArr = question.getOptionB().split("\n");
			if(optionBArr.length > maxOptionSize){
				throw new RuntimeException("最多" + maxOptionSize + "个选项");
			}
			String[] optionCArr = question.getOptionC().split("\n");
			if(optionCArr.length > maxOptionSize){
				throw new RuntimeException("最多" + maxOptionSize + "个选项");
			}
		}
		
		//添加问题
		if(question.getType() == 1 || question.getType() == 2 || question.getType() == 4 
				|| question.getType() == 5 || question.getType() == 7){
			question.setOptionB(null);
			question.setOptionC(null);
		}else if(question.getType() == 3 || question.getType() == 6){
			question.setOptionC(null);
		}else if(question.getType() == 8){
			
		}else if(question.getType() == 9 || question.getType() == 10){
			question.setOptionA(null);
			question.setOptionB(null);
			question.setOptionC(null);
		}
		
		question.setVer(1);
		questionDao.save(question);
		
		question.setNum(question.getId());
		questionDao.update(question);
	}
	
	@Override
	public void updateAndUpdate(Question question, LoginUser user) {
		//校验数据有效性
		Question oldQuestion = getEntity(question.getId());
		if(question.getType() == null){
			throw new RuntimeException("无法获取参数： type");
		}
		if(question.getState() != 1 && question.getState() != 2 ){
			throw new RuntimeException("参数无效： state");
		}
		//1：单选竖向；2：单选横向；3：单选行列式；4：多选竖向；5：多选横向；6：多选行列式；7：下拉列表；8：下拉列表行列式；9：输入框；10：文本框
		int maxOptionSize = DictCache.getIndexDictlistMap().get("QUESTION_OPTIONS").size();
		if(oldQuestion.getType() == 1 || oldQuestion.getType() == 2 || oldQuestion.getType() == 4 
				|| oldQuestion.getType() == 5 || oldQuestion.getType() == 7){
			if(!ValidateUtil.isValid(question.getOptionA())){
				throw new RuntimeException("无法获取参数：optionA");
			}
			
			String[] optionAArr = question.getOptionA().split("\n");
			if(optionAArr.length > maxOptionSize){
				throw new RuntimeException("最多" + maxOptionSize + "个选项");
			}
		}
		if(oldQuestion.getType() == 3 || oldQuestion.getType() == 6){
			if(!ValidateUtil.isValid(question.getOptionA())){
				throw new RuntimeException("无法获取参数：optionA");
			}
			if(!ValidateUtil.isValid(question.getOptionB())){
				throw new RuntimeException("无法获取参数：optionB");
			}
			
			String[] optionAArr = question.getOptionA().split("\n");
			if(optionAArr.length > maxOptionSize){
				throw new RuntimeException("最多" + maxOptionSize + "个选项");
			}
			String[] optionBArr = question.getOptionB().split("\n");
			if(optionBArr.length > maxOptionSize){
				throw new RuntimeException("最多" + maxOptionSize + "个选项");
			}
		}
		if(oldQuestion.getType() == 8){
			if(!ValidateUtil.isValid(question.getOptionA())){
				throw new RuntimeException("无法获取参数：optionA");
			}
			if(!ValidateUtil.isValid(question.getOptionB())){
				throw new RuntimeException("无法获取参数：optionB");
			}
			if(!ValidateUtil.isValid(question.getOptionC())){
				throw new RuntimeException("无法获取参数：optionC");
			}
			
			String[] optionAArr = question.getOptionA().split("\n");
			if(optionAArr.length > maxOptionSize){
				throw new RuntimeException("最多" + maxOptionSize + "个选项");
			}
			String[] optionBArr = question.getOptionB().split("\n");
			if(optionBArr.length > maxOptionSize){
				throw new RuntimeException("最多" + maxOptionSize + "个选项");
			}
			String[] optionCArr = question.getOptionC().split("\n");
			if(optionCArr.length > maxOptionSize){
				throw new RuntimeException("最多" + maxOptionSize + "个选项");
			}
		}
		
		//把修改前的问题删除
		oldQuestion.setState(0);
		
		//把修改后的问题改为新版本，版本加一
		Question newQuesiton = new Question();
		BeanUtils.copyProperties(oldQuestion, newQuesiton);
		newQuesiton.setId(null);
		newQuesiton.setNum(oldQuestion.getNum());
		newQuesiton.setVer(oldQuestion.getVer() + 1);
		newQuesiton.setUpdateTime(new Date());
		newQuesiton.setUpdateUserId(user.getId());
//		newQuesiton.setType(question.getType());//不允许修改类型
		newQuesiton.setState(question.getState());
		newQuesiton.setTitle(question.getTitle());
		newQuesiton.setOptionA(question.getOptionA());
		newQuesiton.setOptionB(question.getOptionB());
		newQuesiton.setOptionC(question.getOptionC());
		
		if(newQuesiton.getType() == 1 || newQuesiton.getType() == 2 || newQuesiton.getType() == 4 
				|| newQuesiton.getType() == 5 || newQuesiton.getType() == 7){
			newQuesiton.setOptionB(null);
			newQuesiton.setOptionC(null);
		}else if(newQuesiton.getType() == 3 || newQuesiton.getType() == 6){
			newQuesiton.setOptionC(null);
		}else if(newQuesiton.getType() == 8){
			
		}else if(newQuesiton.getType() == 9 || newQuesiton.getType() == 10){
			newQuesiton.setOptionA(null);
			newQuesiton.setOptionB(null);
			newQuesiton.setOptionC(null);
		}
		questionDao.save(newQuesiton);
	}
	
	@Override
	public List<Map<String, Object>> getQuestionTypeTreeList() {
		return questionTypeService.getTreeList();
	}

	@Override
	public QuestionType getQuestionType(Integer id) {
		return questionDao.getQuestionType(id);
	}

	@Override
	public void doQuestionTypeUpdate(Integer[] ids, Integer questionTypeId) {
		//校验数据有效性
		if(!ValidateUtil.isValid(ids)){
			throw new RuntimeException("无法获取参数：ids");
		}
		if(questionTypeId == null){
			throw new RuntimeException("无法获取参数：questionTypeId");
		}
		
		//完成设置问题
		for(Integer id : ids){
			Question question = questionDao.getEntity(id);
			if(question.getQuestionTypeId() == questionTypeId){
				continue;
			}
			
			question.setQuestionTypeId(questionTypeId);
			questionDao.update(question);
		}
	}

	@Override
	public List<Question> getList(Integer questionTypeId) {
		return questionDao.getList(questionTypeId);
	}

	@Override
	public void deleteAndUpdate(Integer[] ids) {
		//校验数据有效性
		if(!ValidateUtil.isValid(ids)){
			throw new RuntimeException("无法获取参数：ids");
		}
		
		//删除问题
		for(Integer id : ids){
			Question question = getEntity(id);
			question.setState(0);
			update(question);
		}
	}
}
